#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>

bool GetQueryString(std::string &query_string)
{
    bool result = false;
    std::string method = getenv("METHOD");
    if (method == "GET")
    {
        query_string = getenv("QUERY_STRING");
        result = true;
    }
    else if (method == "POST")
    {
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char c = 0;
        while (content_length)
        {
            read(0, &c, 1);
            query_string.push_back(c);
            content_length--;
        }
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

bool CutString(const std::string &target, std::string &sub1_out, std::string &sub2_out, std::string sep)
{ // 把target以sep为分隔符，切分成sub1_out和sub2_out两部分，目前只获取前两个参数
    size_t pos = target.find(sep);
    if (pos != std::string::npos)
    {
        sub1_out = target.substr(0, pos);
        sub2_out = target.substr(pos + sep.size(), target.find(sep));
        return true;
    }
    return false;
}